package com.zhipu.balance.ui.vm.activate

import android.annotation.SuppressLint
import android.content.Intent
import android.provider.Settings
import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.*
import com.blankj.utilcode.util.ActivityUtils.startActivity
import com.google.gson.Gson
import com.zhipu.balance.common.Constants
import com.zhipu.balance.mobile.ResultEntity
import com.zhipu.balance.mobile.temporaryClass.Activate
import com.zhipu.balance.remotedata.InterceptObserver
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.activate.ActivateActivity
import com.zhipu.balance.ui.home.HomeActivity
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.InitializeUtils.getSignV0
import com.zhipu.balance.utils.dialog.CommonDialog
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.io.File
import java.util.*
import io.reactivex.Observable


/**
 * 激活页面
 */
class ActivateViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){
    /**
     * MAC
     */
    var StringMAC = ObservableField(DeviceUtils.getMacAddress().uppercase(Locale.getDefault()))
    var prefixMAC = "MAC:"


    /**
     * NS
     */
    var serialNumber = ObservableField(android.os.Build.SERIAL)
    var prefixNS = "NS:"
    /**
     * 激活码
     */
    var ActivationCode =  ObservableField("CF6945A1784A4C8E")


    fun WIFISet(){
        val wifiSettingsIntent = Intent(Settings.ACTION_WIFI_SETTINGS)
        wifiSettingsIntent.putExtra("extra_prefs_show_button_bar", true)
        wifiSettingsIntent.putExtra("extra_prefs_set_next_text", "完成")
        startActivity(wifiSettingsIntent)
    }


    @SuppressLint("CheckResult")
    fun ConfirmActivation(){
        if (TextUtils.isEmpty(ActivationCode.get())) {
            InitializeUtils.getDefaultMaker()?.show("请输入激活码")
            return
        }
        var activate = Activate()
        /**
         * mac地址
         */
        activate.mac = StringMAC.get();
        /**
         * mac地址
         */
        activate.androidId = serialNumber.get();

        /**
         * 激活码
         */
        ActivationCode.set(ActivationCode.get()?.uppercase())
        activate.activationCode = ActivationCode.get()
        /**
         * 通用提示
         */
        CommonDialog.showGP("请稍后")
        /**
         *
         */
        val Time = TimeUtils.getNowMills()
        val json = Gson().toJson(activate).toString() + "&" + Time
        val byteSign: ByteArray? = getSignV0()?.signatureParams(json)
        val signature = ConvertUtils.bytes2HexString(byteSign)

        var observable : Observable<ResultEntity<String>> = mSystemApiService.activate(signature,Time,activate)
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : InterceptObserver<String>() {


            /**
             * 成功数据data
             */
            override fun <T> onNextData(resultEntity: ResultEntity<T>?) {
                InitializeUtils.getDefaultMaker()?.show(resultEntity?.msg)
                val mSuccess : Boolean = FileIOUtils.writeFileFromString(Constants.ActivatePath + File.separator + "Activate.dat", GsonUtils.toJson(activate))
                if (mSuccess) {
                    startActivity(HomeActivity::class.java)
                    ActivityUtils.finishActivity(ActivateActivity::class.java)
                }
                /**
                 * 关闭弹框
                 */
                CommonDialog.dismissGP()
            }

            /**
             * 错误网络
             */
            override fun onErrorNet(t: Throwable?, netMsg: String?) {
                InitializeUtils.getDefaultMaker()?.show(netMsg)
                /**
                 * 关闭弹框
                 */
                CommonDialog.dismissGP()

            }
        })
    }
}



